from urllib import request
from lxml import etree

header_dict = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36",
}

def get_http(load_url, header):
    res = ""
    try:
        req = request.Request(url=load_url,headers=header)
        conect = request.urlopen(req)
        byte_res = conect.read()
        try:
            res = byte_res.decode(encoding='utf-8')
        except:
            res = byte_res.decode(encoding='gbk')
    except Exception as e:
        print(e)
    return res

url = "http://bjfx.gov.cn/fsfx/yxqk.html"
page = get_http(url, header_dict)
print(page)
pages = etree.HTML(page)
heads = pages.xpath("//thead//tr//th/text()")
bodys = pages.xpath("//tbody//tr//td/text()|//tbody//tr//td//font/text()")
print(heads)
print(bodys)
